package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.jpa.TblRole;
import com.mastercard.assets.jpa.TblUsuario;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.CatalogoNuevoItemForm;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class EditarDatosUsuarioIn extends Action {

    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws
            IOException,
            ServletException,
            MasterCardException {

        HttpSession session = request.getSession();

        // en caos de null
        if (session == null) {
            return mapping.findForward("noLogin");
        }

        // toma el userLogin
        UserLogin user = (UserLogin) session.getAttribute("UserLogin");

        // en caso de null
        if (user == null) {
            return mapping.findForward("noLogin");
        }

        // obtiene el regId
        int regIdS = user.getTblUsuario().getCveUsuario(); 

        //int i = Integer.parseInt( regIdS ) ;

        TblUsuario res = MasterCardDAO.findUserById(regIdS);

        String pw = MCUtil.decryptPassword(res.getPswd());

        // forma el form
        CatalogoNuevoItemForm f = new CatalogoNuevoItemForm();

        f.setRegId(regIdS);
        f.setCampo1(res.getLogin());
        f.setCampo2(res.getEmail());
        f.setCampo3(res.getNombre());
        f.setCampo4(pw);
        f.setOperation(3);
        f.setCatalogoId(13);
        f.setCampoInt1(res.getTblPais().getCvePais()) ;

        request.setAttribute("EditarUsuarioEditarForm", f);

        MasterCardDAO.closeEntityManager( ) ;
        if( user.getIdioma( ) == 1 ) {
            return mapping.findForward("successEng");
        } else {
            return mapping.findForward("success");
        }
        

    }
}
